<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-breadcrumb>
              <a-breadcrumb-item href="">
                <a-icon type="home" />
                <span>首页</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <a-icon type="user" />
                <span>合同管理</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <span>
                <template v-if="this.id !== ''"> <a-icon type="edit" />合同详情</template>
                  <template v-else> <a-icon type="folder-add" />合同添加</template>
                </span>
              </a-breadcrumb-item>
            </a-breadcrumb>
          </a-col>
          <a-col :span="24">&nbsp;</a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-page-search-wrapper">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="18">
            <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-button-group>
                <a-button type="primary" @click="goBack"> <a-icon type="left" />返回 </a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
          <a-col :span="6" :style="{ textAlign: 'right' }">
            <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-button-group>
                <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
                  <a-button type="primary" icon="import"> 合同明细导入 </a-button>
                </a-upload>
                <a-button type="primary" @click="handleExportXls('合同明细')">合同明细导出 <a-icon type="right" /></a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="id" hidden="true">
            <a-input v-decorator="['id']" />
            </a-form-item>
            <a-form-item label="statusMethod" hidden="true">
              <a-input v-decorator="['statusMethod']" />
            </a-form-item>
            <a-form-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['code']" :disabled="disabledOne" readOnly placeholder="请输入编号"></a-input>
            </a-form-item>
            <a-form-item label="合同号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractCode']" :disabled="disabledOne" placeholder="请输入合同号"></a-input>
            </a-form-item>
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['contractName']" :disabled="disabledOne" placeholder="请输入合同名称"></a-input>
            </a-form-item>
            <a-form-item label="主责部门id" hidden="true">
            <a-input v-decorator="['mainDepartId']" hidden="true" placeholder="请输入主责部门id"></a-input>
            </a-form-item>
            <a-form-item label="主责部门名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mainDepart']" :disabled="disabledOne" placeholder="请输入主责部门名称"></a-input>
            </a-form-item>
            <a-form-item label="招标单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['tenderNumber']" readOnly placeholder="请输入招标单号" >
                <a-icon slot="prefix" type="bars"/>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['createBy']" :disabled="disabledOne" placeholder="请输入负责人"></a-input>
            </a-form-item>
            <a-form-item label="供货商编号" hidden="true">
            <a-input v-decorator="['supplierCode']" hidden="true" placeholder="请输入供货商编号" ></a-input>
            </a-form-item>
            <a-form-item label="供货商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['supplierName']" :disabled="disabledOne" placeholder="请输入供货商名称"></a-input>
            </a-form-item>
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['status']" :disabled="disabledOne" placeholder="请输入状态"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="合同起始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择合同起始时间" :disabled="disabledOne" v-decorator="['startTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="合同终止时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择合同终止时间":disabled="disabledOne"  v-decorator="['endTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
            <a-form-item label="票卡合同" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-checkbox v-decorator="['isTicket']"  :disabled="disabledOne" :checked="isTicket">
               是
              </a-checkbox>
            </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">

    </div>

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" bordered :data-source="dataSource" :columns="columns">
      </a-table>
    </div>

    <mms-contract-details-modal ref="modalForm" @ok="modalFormOk"></mms-contract-details-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@lone/lone-vue/src/utils/mixin'
  import MmsContractDetailsModal from './modules/MmsContractDetailsModal'
  import JDate from '@lone/lone-vue/src/components/jeecg/JDate'
  import { postAction, putAction, downFile, deleteAction, getAction } from '@lone/lone-vue/src/api/manage'
  import Vue from 'vue'

  export default {
    name: 'MmsContractDetailsList',
    mixins:[mixinDevice],
    components: {
      JDate,
      MmsContractDetailsModal
    },
    props:{
      //判断是编辑还是新增
      status:{
        type:String,
        required:false
      },
      //判断是编辑Id
      id:{
        type:String,
        required:false
      },
    },
    watch:{
      status: {
        handler(newVal, oldVal) {
          this.loadFormData()
        },
        immediate: true
      }
    },
    data () {
      return {
        description: 'mms_contract_details管理页面',
        top: 10,
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        // 表头
        disabledAll: false,
        disabledOne: false,
        editingKey: '',
        dataSource: [],
        isTicket: false,
        form: this.$form.createForm(this),
        statusMethod: 'ADD',
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物资编码',
            align:"center",
            dataIndex: 'materialCode'
          },
          {
            title:'物资名称',
            align:"center",
            dataIndex: 'materialName'
          },
          {
            title:'规格',
            align:"center",
            dataIndex: 'specs'
          },
          {
            title:'型号',
            align:"center",
            dataIndex: 'model'
          },
          {
            title:'单价',
            align:"center",
            dataIndex: 'price'
          },
          {
            title:'计量单位',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title:'品牌名称',
            align:"center",
            dataIndex: 'brand'
          },
          {
            title:'技术参数',
            align:"center",
            dataIndex: 'param'
          }
        ],
        url: {
          exportXlsUrl: "/base/mmsContractDetails/exportXls",
          importExcelUrl: "base/mmsContractDetails/importExcel",
        },
        dictOptions:{},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domainURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleChange(e) {
        this.isTicket = e.target.checked;
        this.$nextTick(() => {
          this.form.validateFields(['isTicket'], { force: true });
        });
      },
      loadFormData() {
        let that = this;
        this.dataSource = [];
        getAction('/base/mmsMaterialCode/getUserInfo', {}).then((res) => {
          if (res.success) {
            let result = res.result;
            if(result){
              this.form.setFieldsValue({createBy: result['realname']})
            }
          }
        })
        if(this.id !== ''){
          let paramsObject = {id: this.id};
          getAction('/base/mmsContract/queryContractById', paramsObject).then((res) => {
            if (res.success) {
              let result = res.result;
              Vue.prototype.$copyProperties(that.form, result,{status: '新建', compileTime: Vue.prototype.$formatDate()}, that.dataSource);
            }
          });
        }
        that.disabledOne = true;
      },
      handleImportExcel(info){
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList);
        }
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            // this.$message.success(`${info.file.name} 文件上传成功`);
            if (info.file.response.code === 201) {
              let { message, result: { msg, fileUrl, fileName } } = info.file.response
              let href = window._CONFIG['domianURL'] + fileUrl
              this.$warning({
                  title: message,
                  content: (
                    <div>
                    <span>{msg}</span><br/>
                    <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
                </div>
              )
            })
            } else {
              this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            }
            this.loadFormData();
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      },
      handleExportXls(fileName){
        if(!fileName || typeof fileName != "string"){
          fileName = "导出文件"
        }
        let param = {...this.queryParam};
        if(this.selectedRowKeys && this.selectedRowKeys.length>0){
          param['selections'] = this.selectedRowKeys.join(",")
        }
        console.log("导出参数",param)
        downFile(this.url.exportXlsUrl,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },
      goBack(){
        this.$emit('prevStep')
      },
      initDictConfig(){
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>